package cn.virens.common.mybatis.dynamic_table.annotations;

import cn.virens.common.spring.annotations.spel.VirAnnotationEvaluationContext;
import cn.virens.common.util.exception.APIException;
import org.aopalliance.intercept.MethodInvocation;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.text.split.SplitUtil;
import org.springframework.core.annotation.AnnotationAttributes;

import java.util.List;

public class VirDynamicTableNameContext extends VirAnnotationEvaluationContext {
    public VirDynamicTableNameContext(MethodInvocation invocation, AnnotationAttributes attributes) {
        super(invocation, attributes);
    }

    protected List<List<String>> format(String[] v) throws APIException {
        return ArrayUtil.mapToList(v, this::split);
    }

    protected List<String> split(String str) throws APIException {
        return SplitUtil.splitTrim(str, "=");
    }

    /**
     * 获取注解申明的变量列表
     */
    public List<List<String>> variables() throws APIException {
        return format(attributes.getStringArray("value"));
    }

    /**
     * 是否清理线程变量
     *
     * @return
     * @throws APIException
     */
    public boolean clean() throws APIException {
        return attributes.getBoolean("clean");
    }
}
